********************************************************************************
global basepath ".../Data"
global results ".../Results"
*
cap n clear all
cap n clear matrix
cap n set max_memory 400g
cap n set maxvar 120000
cap n set segmentsize 1g
cap n set max_preservemem 100g
cap n set more off
cap n set checksum off
cap n set type double
cap n set processors 6
cap n version 16.1
********************************************************************************


********************************************************************************
** (A) Identify the Switchers from U-SACCO to Banks
********************************************************************************

clear all
use "$basepath/dataRWA.dta", clear
gcollapse (mean) BANK MFI SACCO, by(bank_id)
gen bank_type=1 if MFI==1
replace bank_type=2 if BANK==1
replace bank_type=3 if SACCO==1
rename bank_id bank_id2
drop BANK MFI SACCO
save "$basepath/bank_type.dta", replace

***

clear all
use "$basepath/dataRWA.dta", clear

*Collapse dataset at the borrower-bank-time level to identify borrowers with a loan at a given point in time
gcollapse (sum) currentbalanceamount, by(borrower_id date_m bank_id)
drop if currentbalanceamount==0

*Identify borrowers with more than one bank at a given point in time
*Then collapse the data to be at the borrower-time level (different bank IDs are now in different variables)
bysort borrower_id date_m: gen n=_n
sum n
forvalues i=1(1)`r(max)'{
	gen bank_`i'=bank_id if n==`i'
}
drop bank_id
gcollapse (mean) bank_*, by(borrower_id date_m)

*Create different auxiliary datasets with the banks a borrower had a relationship with in the past 1, 2 ...., 120 months
*Merge the auxiliary datasets back to the original dataset. In the end it will tell us if the borrower
*had a relationship with a bank in the past, and which bank was that i.e., with the respective bank identifier.
*We need this information to categorize if the switch was from bank to bank, MFI to bank, bank to MFI, or MFI to MFI)
forvalues i=1(1)120 {
	preserve
foreach x of varlist _all {
	rename `x' L`i'_`x'
}
	rename L`i'_borrower_id borrower_id
	save "$basepath/L`i'_date_m.dta", replace
	restore
	*
	preserve
	clear all
	use "$basepath/dataRWA.dta", clear
	gen L`i'_date_m=date_m-`i'
	fmerge m:1 borrower_id L`i'_date_m using "$basepath/L`i'_date_m.dta", keep(master match) nogen
	drop L`i'_date_m
forvalues num = 1(1)6 {
	missings dropvars L`i'_bank_`num', force
}
	save "$basepath/dataRWA.dta", replace
	erase "$basepath/L`i'_date_m.dta"
	restore
}

***

clear all
use "$basepath/dataRWA.dta", clear
*
global varlist L*_bank_* L1*_bank_* L2*_bank_* L3*_bank_* L4*_bank_* L5*_bank_* L6*_bank_* L7*_bank_* L8*_bank_* L9*_bank_* L10*_bank_* L11*_bank_* L12*_bank_*
sum $varlist
egen no_inside_banks=rownvals($varlist)
*
return clear
sum no_inside_banks
forvalues i=1(1)`r(max)'{
	gegen inside_bank_`i'=rowfirst($varlist)
foreach var of varlist $varlist{
	replace `var'=. if `var'==inside_bank_`i'
}
}
drop $varlist
*
xtset ID date_m, monthly
bysort ID: gen new_relationship=1 if L1.currentbalanceamount==. & L2.currentbalanceamount==. & L3.currentbalanceamount==. & L4.currentbalanceamount==. & L5.currentbalanceamount==. & L6.currentbalanceamount==. & L7.currentbalanceamount==. & L8.currentbalanceamount==. & L9.currentbalanceamount==. & L10.currentbalanceamount==. & L11.currentbalanceamount==. & L12.currentbalanceamount==. & L13.currentbalanceamount==. & L14.currentbalanceamount==. & L15.currentbalanceamount==. & L16.currentbalanceamount==. & L17.currentbalanceamount==. & L18.currentbalanceamount==. & L19.currentbalanceamount==. & L20.currentbalanceamount==. & L21.currentbalanceamount==. & L22.currentbalanceamount==. & L23.currentbalanceamount==. & L24.currentbalanceamount==. & L25.currentbalanceamount==. & L26.currentbalanceamount==. & L27.currentbalanceamount==. & L28.currentbalanceamount==. & L29.currentbalanceamount==. & L30.currentbalanceamount==. & L31.currentbalanceamount==. & L32.currentbalanceamount==. & L33.currentbalanceamount==. & L34.currentbalanceamount==. & L35.currentbalanceamount==. & L36.currentbalanceamount==. & L37.currentbalanceamount==. & L38.currentbalanceamount==. & L39.currentbalanceamount==. & L40.currentbalanceamount==. & L41.currentbalanceamount==. & L42.currentbalanceamount==. & L43.currentbalanceamount==. & L44.currentbalanceamount==. & L45.currentbalanceamount==. & L46.currentbalanceamount==. & L47.currentbalanceamount==. & L48.currentbalanceamount==. & L49.currentbalanceamount==. & L50.currentbalanceamount==. & L51.currentbalanceamount==. & L52.currentbalanceamount==. & L53.currentbalanceamount==. & L54.currentbalanceamount==. & L55.currentbalanceamount==. & L56.currentbalanceamount==. & L57.currentbalanceamount==. & L58.currentbalanceamount==. & L59.currentbalanceamount==. & L60.currentbalanceamount==. & L61.currentbalanceamount==. & L62.currentbalanceamount==. & L63.currentbalanceamount==. & L64.currentbalanceamount==. & L65.currentbalanceamount==. & L66.currentbalanceamount==. & L67.currentbalanceamount==. & L68.currentbalanceamount==. & L69.currentbalanceamount==. & L70.currentbalanceamount==. & L71.currentbalanceamount==. & L72.currentbalanceamount==. & L73.currentbalanceamount==. & L74.currentbalanceamount==. & L75.currentbalanceamount==. & L76.currentbalanceamount==. & L77.currentbalanceamount==. & L78.currentbalanceamount==. & L79.currentbalanceamount==. & L80.currentbalanceamount==. & L81.currentbalanceamount==. & L82.currentbalanceamount==. & L83.currentbalanceamount==. & L84.currentbalanceamount==. & L85.currentbalanceamount==. & L86.currentbalanceamount==. & L87.currentbalanceamount==. & L88.currentbalanceamount==. & L89.currentbalanceamount==. & L90.currentbalanceamount==. & L91.currentbalanceamount==. & L92.currentbalanceamount==. & L93.currentbalanceamount==. & L94.currentbalanceamount==. & L95.currentbalanceamount==. & L96.currentbalanceamount==. & L97.currentbalanceamount==. & L98.currentbalanceamount==. & L99.currentbalanceamount==. & L100.currentbalanceamount==. & L101.currentbalanceamount==. & L102.currentbalanceamount==. & L103.currentbalanceamount==. & L104.currentbalanceamount==. & L105.currentbalanceamount==. & L106.currentbalanceamount==. & L107.currentbalanceamount==. & L108.currentbalanceamount==. & L109.currentbalanceamount==. & L110.currentbalanceamount==. & L111.currentbalanceamount==. & L112.currentbalanceamount==. & L113.currentbalanceamount==. & L114.currentbalanceamount==. & L115.currentbalanceamount==. & L116.currentbalanceamount==. & L117.currentbalanceamount==. & L118.currentbalanceamount==. & L119.currentbalanceamount==. & L120.currentbalanceamount==.
*
gen outside_bank=bank_id if new_relationship==1

***

gen switcher=1 if no_inside_banks>0 & new_relationship==1
*
return clear
sum no_inside_banks
forvalues i=1(1)`r(max)'{
	replace switcher=. if inside_bank_`i'==outside_bank
	rename inside_bank_`i' bank_id2
	fmerge m:1 bank_id2 using "$basepath/bank_type.dta"
	drop if _merge==2
	drop _merge
	rename bank_type inside_bank_`i'_type
	rename bank_id2 inside_bank_`i'
}
*
gen bank_id2=bank_id
fmerge m:1 bank_id2 using "$basepath/bank_type.dta"
drop if _merge==2
drop _merge bank_id2
gen outside_bank_type=bank_type if outside_bank!=.
*
global varlist inside_bank_*_type
gegen inside_bank_type=rowmax($varlist)
*
drop inside_bank_*_type new_relationship
replace inside_bank_type=. if switcher==.
replace outside_bank=. if switcher==.
replace outside_bank_type=. if switcher==.
*
gen MFI_to_MFI=1 if outside_bank_type==1 & inside_bank_type==1 & switcher!=.
gen MFI_to_Bank=1 if outside_bank_type==2 & inside_bank_type==1 & switcher!=.
gen MFI_to_SACCO=1 if outside_bank_type==3 & inside_bank_type==1 & switcher!=.
gen Bank_to_MFI=1 if outside_bank_type==1 & inside_bank_type==2 & switcher!=.
gen Bank_to_Bank=1 if outside_bank_type==2 & inside_bank_type==2 & switcher!=.
gen Bank_to_SACCO=1 if outside_bank_type==3 & inside_bank_type==2 & switcher!=.
gen SACCO_to_MFI=1 if outside_bank_type==1 & inside_bank_type==3 & switcher!=.
gen SACCO_to_Bank=1 if outside_bank_type==2 & inside_bank_type==3 & switcher!=.
gen SACCO_to_SACCO=1 if outside_bank_type==3 & inside_bank_type==3 & switcher!=.
sum switcher Bank_to_Bank Bank_to_SACCO Bank_to_MFI SACCO_to_Bank SACCO_to_SACCO SACCO_to_MFI MFI_to_Bank MFI_to_SACCO MFI_to_MFI
*
gen switching_date=date_m if switcher==1
format %tm switching_date
*
gegen first_bank_date_m=min(date_m), by(borrower_id)
format %tm first_bank_date_m
gen first_bank_type2=bank_type if date_m==first_bank_date_m
gegen first_bank_type=max(first_bank_type2), by(borrower_id)
*
gen first_bank2=bank_id if date_m==first_bank_date_m & first_bank_type==bank_type
gegen first_bank=max(first_bank2), by(borrower_id)
drop first_bank_type2 first_bank2
*
gen time_in_banking_system=date_m-first_bank_date_m
*
gen first_bank_is_MFI_date_m2=date_m if bank_type==1
gegen first_bank_is_MFI_date_m=min(first_bank_is_MFI_date_m2), by(borrower_id)
gen time_in_MFI=date_m-first_bank_is_MFI_date_m
replace time_in_MFI=0 if time_in_MFI<0 | time_in_MFI==.
drop first_bank_is_MFI_date_m2 first_bank_is_MFI_date_m
*
gen first_bank_is_bank_date_m2=date_m if bank_type==2
gegen first_bank_is_bank_date_m=min(first_bank_is_bank_date_m2), by(borrower_id)
gen time_in_bank=date_m-first_bank_is_bank_date_m
replace time_in_bank=0 if time_in_bank<0 | time_in_bank==.
drop first_bank_is_bank_date_m2 first_bank_is_bank_date_m
*
gen first_bank_is_SACCO_date_m2=date_m if bank_type==3
gegen first_bank_is_SACCO_date_m=min(first_bank_is_SACCO_date_m2), by(borrower_id)
gen time_in_SACCO=date_m-first_bank_is_SACCO_date_m
replace time_in_SACCO=0 if time_in_SACCO<0 | time_in_SACCO==.
drop first_bank_is_SACCO_date_m2 first_bank_is_SACCO_date_m
*
xtset ID date_m, monthly

***

rename borrower_id crn
preserve
keep if SACCO_to_Bank==1
keep date_m bank_id crn sector post_sector SACCO_to_Bank switching_date outside_bank outside_bank_type switcher inside_bank_1 inside_bank_type first_bank_date_m first_bank_type first_bank time_in_banking_system time_in_bank time_in_MFI time_in_SACCO
keep if time_in_bank==0
drop if time_in_SACCO==0
drop if time_in_banking_system==0
gegen count_switch=count(switcher), by(crn)
gegen min_switch=min(switching_date) if count_switch>1, by(crn)
drop if switching_date!=min_switch & min_switch!=. & switching_date!=.
bysort crn date_m switching_date:  gen dup = cond(_N==1,0,_n)
drop if dup>0
drop dup
gunique crn if SACCO_to_Bank==1
save "$basepath/switchers_SACCO_to_Bank.dta", replace
gcollapse (sum) SACCO_to_Bank, by(sector post_sector)
gen SACCO_to_Bank_Pre=SACCO_to_Bank if post_sector==0
gen SACCO_to_Bank_Post=SACCO_to_Bank if post_sector==1
drop post_sector SACCO_to_Bank
gcollapse (mean) SACCO_to_Bank_Pre SACCO_to_Bank_Post, by(sector)
save "$basepath/switchers_SACCO_to_Bank_by_Sector.dta", replace
clear all
use "$basepath/switchers_SACCO_to_Bank.dta", clear
keep date_m bank_id crn inside_bank_1 SACCO_to_Bank
save "$basepath/switchers_SACCO_to_Bank.dta", replace
restore
*
preserve
keep if MFI_to_Bank==1
keep date_m bank_id crn sector post_sector MFI_to_Bank switching_date outside_bank outside_bank_type switcher inside_bank_type first_bank_date_m first_bank_type first_bank time_in_banking_system time_in_bank time_in_MFI time_in_SACCO
keep if time_in_bank==0
drop if time_in_MFI==0
drop if time_in_banking_system==0
gegen count_switch=count(switcher), by(crn)
gegen min_switch=min(switching_date) if count_switch>1, by(crn)
drop if switching_date!=min_switch & min_switch!=. & switching_date!=.
bysort crn date_m switching_date:  gen dup = cond(_N==1,0,_n)
drop if dup>0
drop dup
gunique crn if MFI_to_Bank==1
save "$basepath/switchers_MFI_to_Bank.dta", replace
gcollapse (sum) MFI_to_Bank, by(sector post_sector)
gen MFI_to_Bank_Pre=MFI_to_Bank if post_sector==0
gen MFI_to_Bank_Post=MFI_to_Bank if post_sector==1
drop post_sector MFI_to_Bank
gcollapse (mean) MFI_to_Bank_Pre MFI_to_Bank_Post, by(sector)
save "$basepath/switchers_MFI_to_Bank_by_Sector.dta", replace
clear all
use "$basepath/switchers_MFI_to_Bank.dta", clear
keep date_m bank_id crn MFI_to_Bank
save "$basepath/switchers_MFI_to_Bank.dta", replace
restore
*
erase "$basepath/bank_type.dta"


********************************************************************************
** (B) Prepare data for switching analysis
********************************************************************************

clear all
use "$basepath/dataRWA_NewLoans.dta", clear

*Merge switchers
rename borrower_id crn
fmerge m:1 date_m bank_id crn using "$basepath/switchers_SACCO_to_Bank.dta", keep(master match) nogen
rename crn borrower_id
gen switcher=1 if SACCO_to_Bank==1
replace switcher=0 if switcher==.
gen switch=1 if switcher==1

*Drop observations without matches in the two baseline scenarios in Table 5
gen group=1 if SACCO==1
replace group=1 if switcher==1
*
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) osample(out)
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
drop if out!=0 & switcher==1
drop out group
*
gen group=1 if BANK==1
replace group=1 if switcher==1
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) osample(out)
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
drop if out!=0 & switcher==1
drop out group
drop if principalamount==. | interestrate==. | maturity==.
*
save "$basepath/switching_analysis.dta", replace

*Isolate switchers that kept the lending relationship at the SACCO
gen switching_loan=switcher
append using "$basepath/all_switcher_loans.dta"
replace switching_loan=0 if switching_loan==.
*
bysort borrower_id: egen max_switch=max(switching_loan)
keep if max_switch==1
drop max_switch
unique borrower_id
unique borrower_id if switching_loan==1
*
gen switching_date=date_m if SACCO_to_Bank==1
bysort borrower_id: ipolate switching_date borrower_id, gen(switchingdate)
*
sort borrower_id date_m
drop if date_m>=switchingdate & switching_loan==0
gen loan_end_m_SACCO=loan_end_m if SACCO==1
bysort borrower_id: egen SACCO_loan_end_m=max(loan_end_m_SACCO)
keep if switching_loan==1
*
gen simultaneous_SACCO_loan=1 if SACCO_loan_end_m>date_m
replace simultaneous_SACCO_loan=0 if simultaneous_SACCO_loan==.
tab simultaneous_SACCO_loan
*
keep date_m bank_id borrower_id simultaneous_SACCO_loan
save "$basepath/simultaneous_SACCO_loan.dta", replace


********************************************************************************
** Figure C5: Probability of Switching, Before and After the U-SACCO Program
********************************************************************************

clear all
use "$basepath/dataRWA.dta", clear
*
rename borrower_id crn
fmerge m:1 date_m bank_id crn using "$basepath/switchers_SACCO_to_Bank.dta", keep(master match) nogen
fmerge m:1 date_m bank_id crn using "$basepath/switchers_MFI_to_Bank.dta", keep(master match) nogen
rename crn borrower_id
*
gen SACCOorMFI_to_Bank=1 if SACCO_to_Bank==1 | MFI_to_Bank==1
egen Total_Borrowers=nvals(borrower_id), by(date_m sector_id)
gcollapse (mean) first_loan_SACCOs Total_Borrowers (sum) SACCOorMFI_to_Bank, by(date_m sector district)
gen sh_SACCOorMFI_to_Bank_TOTborr=SACCOorMFI_to_Bank/(Total_Borrowers/1000)
*
egen sector_id=concat(sector district), p(,)
drop sector district
encode sector_id, gen(sector_district)
xtset sector_district date_m, monthly
tsfill, full
replace sh_SACCOorMFI_to_Bank_TOTborr=0 if sh_SACCOorMFI_to_Bank_TOTborr==.
drop sector_id
decode sector_district, gen(sector_id)
split sector_id, p(,)
rename sector_id1 sector
rename sector_id2 district
drop sector_district sector_id
gegen First_loan_SACCOs=mean(first_loan_SACCOs), by(sector district)
format %tm First_loan_SACCOs
drop first_loan_SACCOs
rename First_loan_SACCOs first_loan_SACCOs
order district sector date_m first_loan_SACCOs
sort district sector date_m
*
gen time_since_post=date_m-first_loan_SACCOs
gen post=(time_since_post>=0 & time_since_post!=.)
gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
gen post_m1y=0
gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
gen post_7y=(time_since_post>=37 & time_since_post!=.)
egen sector_id=group(district sector)
drop time_since_post
order district sector sector_id
*
cap n reghdfe sh_SACCOorMFI_to_Bank_TOTborr post_*, absorb(date_m sector_id i.sector_id##c.date_m) cluster(sector_id)
cap n estimates store switching_plot
coefplot (switching_plot, offset(0)), keep(post_m7y post_m6y post_m5y post_m4y post_m3y post_m2y post_m1y post_1y post_2y post_3y post_4y post_5y post_6y post_7y) byopts(yrescale) vertical yline(0, lcolor(black) lwidth(thin) lpattern(dash)) xline(7.5, lcolor(black) lwidth(thin) lpattern(dash)) levels(90) omitted
graph save Graph "$results/Figure_C5.gph", replace
graph export "$results/Figure_C5.pdf", as(pdf) replace
graph export "$results/Figure_C5.png", as(png) replace


********************************************************************************
** Tables 5-7, B1, and C10-C18: Analysis of Switching Loans
********************************************************************************

* (A1) Control Group: New loans by U-SACCOs to non-switcher borrowers in the same month

clear all
use "$basepath/switching_analysis.dta", clear
*
merge m:1 date_m bank_id borrower_id using "$basepath/simultaneous_SACCO_loan.dta"
drop if _merge==2
drop _merge
*
gen group=1 if SACCO==1
replace group=1 if switcher==1
*
egen group2=group(group date_m)
*
cls
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
*
qui logit switcher $closest_match_vars $exact_match_vars if group2!=.
qui predict pscore if e(sample), pr
qui replace pscore=(group2*1000)+pscore
psmatch2 switcher, pscore(pscore) outcome(principalamount)
qui drop pscore
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
*
qui logit switcher $closest_match_vars $exact_match_vars if group2!=.
qui predict pscore if e(sample), pr
qui replace pscore=(group2*1000)+pscore
psmatch2 switcher, pscore(pscore) outcome(interestrate)
qui drop pscore
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (maturity $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
*
qui logit switcher $closest_match_vars $exact_match_vars if group2!=.
qui predict pscore if e(sample), pr
qui replace pscore=(group2*1000)+pscore
psmatch2 switcher, pscore(pscore) outcome(maturity)
qui drop pscore
***
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_1y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
****
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_2y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_ty $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
*
preserve
replace switcher=. if simultaneous_SACCO_loan==0
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_1y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_2y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_ty $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
restore
*
preserve
replace switcher=. if simultaneous_SACCO_loan==1
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_1y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_2y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_ty $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
restore

* (A2) Control Group: New loans by switcher's previous U-SACCO to non-switcher borrowers in the same month

clear all
use "$basepath/switching_analysis.dta", clear
*
gen group=1 if SACCO==1
replace group=1 if switcher==1
*
gen bank_id2=bank_id
replace bank_id2=inside_bank_1 if switcher==1
***
cls
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group bank_id2 date_m
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) osample(out)
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group bank_id2 date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group bank_id2 date_m
cap n teffects nnmatch (maturity $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)


* (B1) Control Group: New loans by commercial banks to non-switcher borrowers in the same month

clear all
use "$basepath/switching_analysis.dta", clear
*
merge m:1 date_m bank_id borrower_id using "$basepath/simultaneous_SACCO_loan.dta"
drop if _merge==2
drop _merge
*
gen group=1 if BANK==1
replace group=1 if switcher==1
*
egen group2=group(group date_m)
***
cls
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
*
qui logit switcher $closest_match_vars $exact_match_vars if group2!=.
qui predict pscore if e(sample), pr
qui replace pscore=(group2*1000)+pscore
psmatch2 switcher, pscore(pscore) outcome(principalamount)
qui drop pscore
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
*
qui logit switcher $closest_match_vars $exact_match_vars if group2!=.
qui predict pscore if e(sample), pr
qui replace pscore=(group2*1000)+pscore
psmatch2 switcher, pscore(pscore) outcome(interestrate)
qui drop pscore
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (maturity $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
*
qui logit switcher $closest_match_vars $exact_match_vars if group2!=.
qui predict pscore if e(sample), pr
qui replace pscore=(group2*1000)+pscore
psmatch2 switcher, pscore(pscore) outcome(maturity)
qui drop pscore
***
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_1y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
****
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_2y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_ty $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
*
preserve
replace switcher=. if simultaneous_SACCO_loan==0
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_1y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_2y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_ty $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
restore
*
preserve
replace switcher=. if simultaneous_SACCO_loan==1
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_1y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_2y $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_ty $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
restore

* (B2) Control Group: New loans by same bank the SACCO client switched to other borrowers in same month

clear all
use "$basepath/switching_analysis.dta", clear
*
merge m:1 date_m bank_id borrower_id using "$basepath/simultaneous_SACCO_loan.dta"
drop if _merge==2
drop _merge
*
gen group=1 if BANK==1
replace group=1 if switcher==1
***
cls
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group bank_id date_m
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables osample(out)
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group bank_id date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group bank_id date_m
cap n teffects nnmatch (maturity $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)

* (B3) Control Group: New loans by commercial banks to first-time borrowers in the same month

clear all
use "$basepath/switching_analysis.dta", clear
*
sort borrower_id date_m
by borrower_id: gen n=_n
replace n=. if n>1
gen bank_type=1 if SACCO==1
replace bank_type=2 if BANK==1
replace bank_type=3 if MFI==1
bysort borrower_id date_m: egen count=nvals(bank_type)
replace n=. if count>1
gen SACCO2=SACCO if n==1
gen MFI2=MFI if n==1
gen BANK2=BANK if n==1
bysort borrower_id: egen max_SACCO=max(SACCO2)
bysort borrower_id: egen max_MFI=max(MFI2)
bysort borrower_id: egen max_BANK=max(BANK2)
gen first_time_bank=1 if BANK==1 & max_MFI==0 & max_SACCO==0 & time_in_banking_sector==0
gen first_time_SACCO=1 if SACCO==1 & max_MFI==0 & max_BANK==0 & time_in_banking_sector==0
bysort borrower_id: egen max_first_time_SACCOs=max(first_time_SACCO)
bysort date_m: egen months_with_switches=max(switch)
*
gen group=first_time_bank
replace group=1 if switcher==1
*
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural mortgage collateral
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) osample(out)
drop if out!=0 & switcher==1
drop out
cls
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural mortgage collateral
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural mortgage collateral
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural mortgage collateral
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (maturity $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)

***

preserve
global controls principalamount interestrate maturity female single young gvt_employee growth nlights_2010 poverty_index bank_presence population_by_sector conflicts duration rural
drop switcher
gen switcher=1 if first_time_SACCO==1
replace switcher=0 if first_time_bank==1
order switcher $controls
keep if switcher==1 | first_time_bank==1
keep switcher $controls
*
gen vname = ""
foreach rhsv of varlist switcher {
gen `rhsv'0 = .
gen `rhsv'1 = .
gen `rhsv'_c = .
gen `rhsv'_t = .
gen `rhsv'_s = ""
}
*
local i=1
foreach var of global controls {
replace vname = "`var'" in `i'
foreach rhsv of varlist switcher {
mean `var', over(`rhsv')
replace `rhsv'0 = _b[c.`var'@0.`rhsv'] in `i'
replace `rhsv'1 = _b[c.`var'@1.`rhsv'] in `i'
replace `rhsv'_c = e(N) in `i'
test _b[c.`var'@0.`rhsv']=_b[c.`var'@1.`rhsv']
replace `rhsv'_t=r(p) in `i'
}
local i=`i'+1
}
keep vname switcher switcher0 switcher1 switcher_c switcher_t switcher_s
forvalues j=1/`=_N'{
foreach rhsv of varlist switcher {
local pval=`rhsv'_t[`j']
if `pval' <=0.01 {
replace `rhsv'_s="***" in `j'
}
else if `pval' <=0.05 {
replace `rhsv'_s="**" in `j'
}
 
else if `pval' <=0.10 {
replace `rhsv'_s="*" in `j'
}
}
} 
format switcher_c %12.0fc 
format switcher0 switcher1 %12.4f
format switcher_t %10.4f
keep vname switcher_c switcher0 switcher1 switcher_t switcher_s
order vname switcher_c switcher0 switcher1 switcher_t switcher_s
drop if switcher_c==.
*
export excel using "$results/Table_C10.xls", replace firstrow(variables)
restore

* (B4) Control Group: New loans by commercial banks to switchers from other MFIs in the same month

clear all
use "$basepath/switching_analysis.dta", clear
*
rename borrower_id crn
merge m:1 date_m bank_id crn using "$basepath/switchers_MFI_to_Bank.dta"
drop if _merge==2
drop _merge
rename crn borrower_id
*
gen group=1 if MFI_to_Bank==1
replace group=1 if switcher==1
*
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) osample(out)
drop if out!=0 & switcher==1
drop out
cls
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (maturity $closest_match_vars) (switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)


********************************************************************************
** Table C15: Analysis of Switching Loans - Subsequent Loans at the Bank
********************************************************************************

clear all
use "$basepath/switching_analysis.dta", clear
*
gen switching_loan=switcher
append using "$basepath/all_switcher_loans.dta"
replace switching_loan=0 if switching_loan==.
*
bysort borrower_id: egen max_switch=max(switching_loan)
keep if max_switch==1
drop max_switch
unique borrower_id
unique borrower_id if switching_loan==1
*
gen switching_date=date_m if SACCO_to_Bank==1
bysort borrower_id: ipolate switching_date borrower_id, gen(switchingdate)
drop if date_m<switchingdate
tab switching_loan
*
gen switchers=switching_loan+1
replace switchers=0 if switchers==2
*
merge m:1 date_m using "$basepath/Repo_lending_rate_monthly.dta"
drop if _merge==2
drop _merge
sum interestrate repo_rate lending_rate
gen spread_interestrate=interestrate-repo_rate
drop switcher
rename switchers switcher
*
global closest_match_vars
global exact_match_vars borrower_id bank_id
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher), atet vce(iid) ematch($exact_match_vars) biasadj($closest_match_vars) osample(out)
cap n teffects nnmatch (principalamount $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($exact_match_vars) biasadj($closest_match_vars) dmvariables
cap n drop out
dis e(n1)
dis e(n0)
*
global closest_match_vars
global exact_match_vars borrower_id bank_id
cap n teffects nnmatch (spread_interestrate $closest_match_vars) (switcher), atet vce(iid) ematch($exact_match_vars) biasadj($closest_match_vars) osample(out)
cap n teffects nnmatch (spread_interestrate $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($exact_match_vars) biasadj($closest_match_vars) dmvariables
cap n drop out
dis e(n1)
dis e(n0)
*
global closest_match_vars
global exact_match_vars borrower_id bank_id
cap n teffects nnmatch (maturity $closest_match_vars) (switcher), atet vce(iid) ematch($exact_match_vars) biasadj($closest_match_vars) osample(out)
cap n teffects nnmatch (maturity $closest_match_vars) (switcher) if out==0, atet vce(iid) ematch($exact_match_vars) biasadj($closest_match_vars) dmvariables
cap n drop out
dis e(n1)
dis e(n0)


********************************************************************************
** Table C11: Analysis of Switching Loans - Summary Statistics
********************************************************************************

clear all
use "$basepath/switching_analysis.dta", clear
*
preserve
*
global controls female single young gvt_employee previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount growth nlights_2010 poverty_index bank_presence population_by_sector conflicts duration rural 
order switcher $controls
keep if switcher==1 | SACCO==1
keep switcher $controls
*
gen vname = ""
foreach rhsv of varlist switcher {
gen `rhsv'0 = .
gen `rhsv'1 = .
gen `rhsv'_c = .
gen `rhsv'_t = .
gen `rhsv'_s = ""
}
*
local i=1
foreach var of global controls {
replace vname = "`var'" in `i'
foreach rhsv of varlist switcher {
mean `var', over(`rhsv')
replace `rhsv'0 = _b[c.`var'@0.`rhsv'] in `i'
replace `rhsv'1 = _b[c.`var'@1.`rhsv'] in `i'
replace `rhsv'_c = e(N) in `i'
test _b[c.`var'@0.`rhsv']=_b[c.`var'@1.`rhsv']
replace `rhsv'_t=r(p) in `i'
}
local i=`i'+1
}
keep vname switcher switcher0 switcher1 switcher_c switcher_t switcher_s
forvalues j=1/`=_N'{
foreach rhsv of varlist switcher {
local pval=`rhsv'_t[`j']
if `pval' <=0.01 {
replace `rhsv'_s="***" in `j'
}
else if `pval' <=0.05 {
replace `rhsv'_s="**" in `j'
}
 
else if `pval' <=0.10 {
replace `rhsv'_s="*" in `j'
}
}
} 
format switcher_c %12.0fc 
format switcher0 switcher1 %12.4f
format switcher_t %10.4f
keep vname switcher_c switcher0 switcher1 switcher_t switcher_s
order vname switcher_c switcher0 switcher1 switcher_t switcher_s
drop if switcher_c==.
*
export excel using "$results/Table_C11_PanelA.xls", replace firstrow(variables)
restore

***

preserve
*
global controls female single young gvt_employee previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount growth nlights_2010 poverty_index bank_presence population_by_sector conflicts duration rural 
order switcher $controls
keep if switcher==1 | BANK==1
keep switcher $controls
*
gen vname = ""
foreach rhsv of varlist switcher {
gen `rhsv'0 = .
gen `rhsv'1 = .
gen `rhsv'_c = .
gen `rhsv'_t = .
gen `rhsv'_s = ""
}
*
local i=1
foreach var of global controls {
replace vname = "`var'" in `i'
foreach rhsv of varlist switcher {
mean `var', over(`rhsv')
replace `rhsv'0 = _b[c.`var'@0.`rhsv'] in `i'
replace `rhsv'1 = _b[c.`var'@1.`rhsv'] in `i'
replace `rhsv'_c = e(N) in `i'
test _b[c.`var'@0.`rhsv']=_b[c.`var'@1.`rhsv']
replace `rhsv'_t=r(p) in `i'
}
local i=`i'+1
}
keep vname switcher switcher0 switcher1 switcher_c switcher_t switcher_s
forvalues j=1/`=_N'{
foreach rhsv of varlist switcher {
local pval=`rhsv'_t[`j']
if `pval' <=0.01 {
replace `rhsv'_s="***" in `j'
}
else if `pval' <=0.05 {
replace `rhsv'_s="**" in `j'
}
 
else if `pval' <=0.10 {
replace `rhsv'_s="*" in `j'
}
}
} 
format switcher_c %12.0fc 
format switcher0 switcher1 %12.4f
format switcher_t %10.4f
keep vname switcher_c switcher0 switcher1 switcher_t switcher_s
order vname switcher_c switcher0 switcher1 switcher_t switcher_s
drop if switcher_c==.
*
export excel using "$results/Table_C11_PanelB.xls", replace firstrow(variables)
restore


********************************************************************************
** Table C16: Analysis of Switchers' Default Risk At U-SACCOs Before Switching
********************************************************************************

clear all
use "$basepath/switching_analysis.dta", clear
*
gen switching_loan=switcher
append using "$basepath/all_switcher_loans.dta"
replace switching_loan=0 if switching_loan==.
*
gen switch_date=date_m if switcher==1
gegen switching_date=max(switch_date), by(borrower_id)
gegen previous_loan_switcher=max(switching_loan), by(borrower_id)
drop if switching_loan==1
drop if previous_loan_switcher==1 & date_m>=switching_date
drop switching_loan switch_date switching_date
gen group=1 if SACCO==1
replace group=1 if previous_loan_switcher==1
*
foreach var of varlist bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural{
gegen `var'm=mean(`var'), by(sector_id)
drop `var'
rename `var'm `var'
}
*
foreach var of varlist previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount npl_L1y npl_L2y npl_Lty{
replace `var'=0 if `var'==.
}
*
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_1y $closest_match_vars) (previous_loan_switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables osample(out)
drop if out!=0 & previous_loan_switcher==1
drop out
cap n teffects nnmatch (npl_1y $closest_match_vars) (previous_loan_switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
****
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_2y $closest_match_vars) (previous_loan_switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)
***
global closest_match_vars principalamount interestrate maturity bank_presence growth nlights_2010 poverty_index population_by_sector conflicts duration rural previous_time_inBS previous_no_bankR previous_no_loans previous_cbamount mortgage collateral npl_L1y npl_L2y npl_Lty
global exact_match_vars female single young gvt_employee
global group_vars group date_m
cap n teffects nnmatch (npl_ty $closest_match_vars) (previous_loan_switcher), atet vce(iid) ematch($group_vars $exact_match_vars) biasadj($closest_match_vars) dmvariables
dis e(n1)
dis e(n0)


********************************************************************************
** Table C17: Lending Constraints at U-SACCOs
********************************************************************************

clear all
use "$basepath/dataRWA_NewLoans.dta", clear
*
fmerge m:1 sector district using "$basepath/sacco_BS_2010.dta", keep(master match) nogen
fmerge m:1 bank_id using "$basepath/bank_BS_2010.dta", keep(master match) nogen
*
gen CR_2010=capital_ratio_10 if SACCO==1
replace CR_2010=B_capital_ratio_10 if BANK==1
*
gen LR_2010=liquidity_ratio_10 if SACCO==1
replace LR_2010=B_liquidity_ratio1_10 if BANK==1
*
gen Ldeposits_10=deposits_10
gen B_Ldeposits_10=B_deposits_10*1000
gen TD_2010=Ldeposits_10 if SACCO==1
replace TD_2010=B_Ldeposits_10 if BANK==1
replace TD_2010=ln(TD_2010/1000000)
*
drop if currentbalanceamount==. | interestrate==. | maturity==.
*
cd "$results"
cap n erase "Table_C17.xls"
global vars currentbalanceamount interestrate maturity
global individual_controls female single young gvt_employee
foreach var of global vars{
reghdfe `var' SACCO  $individual_controls if TD_2010!=. & CR_2010!=. & LR_2010!=., absorb(sector_id##date_m) noconstant keepsingletons cluster(sector_id)
outreg2 using "Table_C17.xls", append adjr2 dec(3) nocons keep(SACCO)
reghdfe `var' SACCO TD_2010 CR_2010 LR_2010 $individual_controls, absorb(sector_id##date_m) noconstant keepsingletons cluster(sector_id)
outreg2 using "Table_C17.xls", append adjr2 dec(3) nocons keep(SACCO TD_2010 CR_2010 LR_2010)
}
cap n erase "Table_C17.txt"

